/** ###################################################################
**     Filename  : Tach.C
**     Project   : project_xgate
**     Processor : MC9S12XDT512BMFU
**     Compiler  : CodeWarrior HCS12X C Compiler
**     Date/Time : 3/25/2009, 2:53 AM
**     Contents  :
**         User source code
**
**     (c) Copyright UNIS, a.s. 1997-2008
**     UNIS, a.s.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/

/* MODULE Tach */
#include "Tach.h"

#include "RPM.h"

#define DEBUG 0

#define TACHAVG 5



/*Interrupt Process Function*/
void Tach_Init(void){
  RPM_Enable();
  DDRT_DDRT6 = 1;  
  DDRT_DDRT5 = 1;
  Tach_Fifo_Init();
}

void Tach_Process(void){             
  RPM_TCapturedValue time;
  dword avgtime=0;
  byte i;
  
  static word values[TACHAVG];
  static byte head=0;
  
  word rpm;
  
  PTT_PTT6 = ~PTT_PTT6;    
  RPM_GetCaptureValue(&time);  
  RPM_Reset();               
  
  values[head] = time;
  head = (head+1)%TACHAVG;
  
  for(i=0;i<TACHAVG;i++){
    avgtime+=values[i];   
  }   
  avgtime/=TACHAVG;
  rpm = (short)(22500000/avgtime);
  if(Tach_Fifo_Put(rpm)) {
  }
  
}


/* END Accel */
